package com.fuze.controller;

import com.fuze.aspect.anno.AuthAdmin;
import com.fuze.common.Advice;
import com.fuze.common.CustomException;
import com.fuze.common.Result;
import com.fuze.common.constant.Constant;
import com.fuze.domain.dto.*;
import com.fuze.service.GuaranteedShareContractService;
import com.fuze.service.MinimumGuaranteeContractService;
import com.fuze.service.ServiceFeeContractService;
import com.fuze.service.SharingContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.sql.ResultSet;

/**
 * @author sepc_
 * @date 5/19/2021 13:59
 * 合同协议管理Controller
 */
@Api(tags = "协议管理接口")
@RestController
@Slf4j
@RequiredArgsConstructor
public class ContractController {

    final HttpServletRequest request;

    /**
     * 分成方式的合同的业务处理
     */
    final SharingContractService sharingContractService;

    /**
     * 保底方式的合同的业务处理
     */
    final MinimumGuaranteeContractService minimumGuaranteeContractService;

    /**
     * 保底分成方式的合同的业务处理
     */
    final GuaranteedShareContractService guaranteedShareContractService;

    /**
     * 服务费用方式的合同的的业务处理
     */
    final ServiceFeeContractService serviceFeeContractService;


    @AuthAdmin
    @ApiOperation(value = "根据合同类型查询合同")
    @PostMapping("/contract/query")
    public Result queryByType(@Validated @RequestBody ContractQueryDTO queryDTO) {
        int type = queryDTO.getType();
        switch (type) {
            case Constant.PARTITION_TYPE:
                return Result.ok(sharingContractService.queryByType(queryDTO));
            case Constant.GUARANTEED_TYPE:
                return Result.ok(minimumGuaranteeContractService.queryByType(queryDTO));
            case Constant.GUARANTEED_SHARE_TYPE:
                return Result.ok(guaranteedShareContractService.queryByType(queryDTO));
            case Constant.SERVICE_CHARGE_TYPE:
                return Result.ok(serviceFeeContractService.queryByType(queryDTO));
            default:
                throw new CustomException(Advice.WRONG_CONTRACT_TYPE);
        }
    }


    @ApiOperation(value = "根据合同类型以及合同id查询合同")
    @AuthAdmin
    @GetMapping("/contract/{type:\\d+}/{id:\\d+}")
    public Result findById(@PathVariable(name = "type") Integer type, @PathVariable(name = "id") Integer id) {
        switch (type) {
            case Constant.PARTITION_TYPE:
                return Result.ok(sharingContractService.findById(id));
            case Constant.GUARANTEED_TYPE:
                return Result.ok(minimumGuaranteeContractService.findById(id));
            case Constant.GUARANTEED_SHARE_TYPE:
                return Result.ok(guaranteedShareContractService.findById(id));
            case Constant.SERVICE_CHARGE_TYPE:
                return Result.ok(serviceFeeContractService.findById(id));
            default:
                throw new CustomException(Advice.WRONG_CONTRACT_TYPE);
        }
    }

    @ApiIgnore
    @ApiOperation(value = "新增分成方式的合同")
    @PostMapping("/contract/partition/add")
    public Result add(@RequestBody @Validated SharingContractDTO sharingContractDTO) {
        int insert = this.sharingContractService.add(sharingContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_INSERTED);
        }
        return Result.ok();
    }

    @ApiIgnore
    @ApiOperation(value = "新增保底方式的合同")
    @PostMapping("/contract/guaranteed/add")
    public Result add(@RequestBody @Validated MinimumGuaranteeContractDTO minimumGuaranteeContractDTO) {
        int insert = this.minimumGuaranteeContractService.add(minimumGuaranteeContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_INSERTED);
        }
        return Result.ok();
    }

    @ApiIgnore
    @ApiOperation(value = "新增保底分成方式的合同")
    @PostMapping("/contract/guaranteedShare/add")
    public Result add(@RequestBody @Validated GuaranteedShareContractDTO guaranteedShareContractDTO) {
        int insert = this.guaranteedShareContractService.add(guaranteedShareContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_INSERTED);
        }
        return Result.ok();
    }

    @ApiIgnore
    @ApiOperation(value = "新增服务费方式的合同")
    @PostMapping("/contract/serviceCharge/add")
    public Result add(@RequestBody @Validated ServiceFeeContractDTO serviceFeeContractDTO) {
        int insert = this.serviceFeeContractService.add(serviceFeeContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_INSERTED);
        }
        return Result.ok();
    }


    @AuthAdmin
    @ApiOperation(value = "更新分成方式的合同")
    @PostMapping("/contract/partition/update")
    public Result update(@RequestBody @Validated SharingContractDTO sharingContractDTO) {
        this.checkId(sharingContractDTO.getId());
        int insert = this.sharingContractService.update(sharingContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }

    @AuthAdmin
    @ApiOperation(value = "更新保底方式的合同")
    @PostMapping("/contract/guaranteed/update")
    public Result update(@RequestBody @Validated MinimumGuaranteeContractDTO minimumGuaranteeContractDTO) {
        this.checkId(minimumGuaranteeContractDTO.getId());
        int insert = this.minimumGuaranteeContractService.update(minimumGuaranteeContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }

    @AuthAdmin
    @ApiOperation(value = "更新保底分成方式的合同")
    @PostMapping("/contract/guaranteedShare/update")
    public Result update(@RequestBody @Validated GuaranteedShareContractDTO guaranteedShareContractDTO) {
        this.checkId(guaranteedShareContractDTO.getId());
        int insert = this.guaranteedShareContractService.update(guaranteedShareContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }

    @AuthAdmin
    @ApiOperation(value = "更新服务费方式的合同")
    @PostMapping("/contract/serviceCharge/update")
    public Result update(@RequestBody @Validated ServiceFeeContractDTO serviceFeeContractDTO) {
        this.checkId(serviceFeeContractDTO.getId());
        int insert = this.serviceFeeContractService.update(serviceFeeContractDTO);
        if (insert != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }


    /**
     * 校验数据传输对象的id是否为空
     *
     * @param id 自增主键
     */
    public void checkId(Integer id) {
        if (id == null) {
            throw new CustomException(Advice.ID_CANNOT_BE_NULL);
        }
    }


    @AuthAdmin
    @ApiOperation(value = "根据合同类型以及合同Id删除合同")
    @DeleteMapping("/contract/delete/{type:\\d+}/{ids}")
    public Result deleteByIds(@PathVariable(name = "type") Integer type,
                              @PathVariable(name = "ids") @Validated
                              @NotNull(message = "需要删除的合同Id不能为空") Integer[] ids) {
        int delete;
        switch (type) {
            case Constant.PARTITION_TYPE:
                delete = sharingContractService.delete(ids);
                break;
            case Constant.GUARANTEED_TYPE:
                delete = minimumGuaranteeContractService.delete(ids);
                break;
            case Constant.GUARANTEED_SHARE_TYPE:
                delete = guaranteedShareContractService.delete(ids);
                break;
            case Constant.SERVICE_CHARGE_TYPE:
                delete = serviceFeeContractService.delete(ids);
                break;
            default:
                throw new CustomException(Advice.WRONG_CONTRACT_TYPE);
        }
        if (delete != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }
}
